package com.sita.linboard.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.driverInfo.VerificationCodeBean;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.ToastUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/26.
 */
public class RegisteredFirstActivity extends BaseActivity implements View.OnClickListener {
    private Button nextstep;
    private EditText registerPhone, registerCode, registerPass, registerConfirmPass;
    private TextView codeTx;
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            codeTx.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
            codeTx.setText("获取");
        }
    };

    public static void RegisterFirstIntent(Context context) {
        Intent intent = new Intent(context, RegisteredFirstActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initData() {
        nextstep.setOnClickListener(this);
        codeTx.setOnClickListener(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        nextstep = bindView(R.id.next_step);
        registerPhone = bindView(R.id.register_phone);
        registerCode = bindView(R.id.register_code);
        registerPass = bindView(R.id.register_pass);
        registerConfirmPass = bindView(R.id.register_confirm_pass);
        codeTx = bindView(R.id.reigster_codeTx);
    }

    @Override
    protected int getId() {
        return R.layout.activity_registerfirst;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_step:
                //下一步
                ReadytoNext();
                break;
            case R.id.reigster_codeTx:
                //获取验证码
                if (registerPhone.getText().length() == 0) {
                    ToastUtils.show("请输入手机号", 3);
                } else {
                    RestClient.getRestService().getVerificationCode(registerPhone.getText().toString(), 0, new Callback<VerificationCodeBean>() {
                        @Override
                        public void success(VerificationCodeBean verificationCodeBean, Response response) {

                        }

                        @Override
                        public void failure(RetrofitError error) {

                        }
                    });
                }
                timer.start();
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        timer.onFinish();
    }

    private void ReadytoNext() {
        if (registerPhone.getText().length() == 0
                || registerCode.getText().length() == 0
                || registerPass.getText().length() == 0
                || registerConfirmPass.getText().length() == 0) {
            ToastUtils.show("输入信息有误", 3);
            return;
        } else {
            if (registerPhone.getText().length() > 0) {
                if (BaseActivity.isPhoneNumberValid(registerPhone.getText().toString())) {
//                    if (registerCode.getText().toString().equals("265307")){
                    if (registerPass.getText().toString().equals(registerConfirmPass.getText().toString())) {
                        RegisterRequest request = new RegisterRequest();
                        request.mobile = registerPhone.getText().toString();
                        request.password = registerPass.getText().toString();
                        request.smsCode = registerCode.getText().toString();
                        request.userType = 0;
                        request.registerType = 2;
                        RestClient.getRestService().register(request, new Callback<RegisterBackBean>() {
                            @Override
                            public void success(RegisterBackBean registerBackBean, Response response) {
                                if (registerBackBean.getErrorCode().equals("0") && registerBackBean.getData() != null) {
                                    ToastUtils.show("注册成功", 3);
                                    //TODO  进入第二部

                                    RegisterSecondActivity.RegisterSecondIntent(RegisteredFirstActivity.this, registerPhone.getText().toString());

                                }
                            }

                            @Override
                            public void failure(RetrofitError error) {
                                ToastUtils.show("该用户已注册", 3);
                                RegisterSecondActivity.RegisterSecondIntent(RegisteredFirstActivity.this, registerPhone.getText().toString());
                            }

                        });
//                        }
//                        else {
//                            ToastUtils.show("验证密码出错",2);
//                        }
                    } else {
                        ToastUtils.show("验证码输入有误，请重新输入!", 3);
                    }
                } else {
                    ToastUtils.show("手机号输入有误，请重新输入！", 3);
                    return;
                }
            }
        }

    }
}
